package myJs.admin

import myJs.tool.Tool.{layerOptions, myElement}
import myJs.Utils._
import scala.scalajs.js.Dynamic.{global => g}

import myJs.myPkg.bootstrap.Bootstrap.default._
import myJs.myPkg.jquery._
import myJs.myPkg.{LayerOptions, Swal, SwalOptions}
import org.scalajs.dom.{Element, FormData, document}
import org.scalajs.dom.ext.Ajax
import scalatags.Text.all._

import scala.scalajs.js
import scala.scalajs.js.JSON
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import scala.concurrent.ExecutionContext.Implicits.global
import myJs.Implicits._
import myJs.tool.Tool
import shared.VarTool

/**
 * Created by yz on 2019/4/25
 */
@JSExportTopLevel("PosSite")
object PosSite {

  @JSExport("init")
  def init = {
    getPosSiteInfo
  }

  def getPosSiteInfo = {
    val url = g.jsRoutes.controllers.AdminController.getPosSiteInfo().url.toString
    Ajax.get(url = s"${url}").map { xhr =>
      val dicts = JSON.parse(xhr.responseText).toJArJDS
      refreshStr(dicts)
      refreshSnp(dicts)
    }
  }

  val sexMap = Map("男" -> "male", "女" -> "female")

  def getSexId(sex: String) = {
    sexMap(sex)
  }

  def refreshStr(dicts: js.Array[js.Dictionary[String]]) = {
    val siteNames = List("Amelogenin", "SRY", "Y-indel", "CSF1PO", "D10S1248", "D10S1435", "D12S391", "D13S317", "D16S539", "D17S1301", "D18S51", "D19S433", "D1S1656", "D20S482", "D21S11", "D22S1045", "D2S1338", "D2S441", "D3S1358", "D4S2408", "D5S818", "D6S1043", "D7S820", "D8S1179", "D9S1122", "FGA", "Penta-D", "Penta-E", "TH01", "TPOX", "vWA", "DYF387S1a/b", "DYF404S1a/b", "DYS19", "DYS385a/b", "DYS388", "DYS389I", "DYS389II", "DYS390", "DYS391", "DYS392", "DYS393", "DYS437", "DYS438", "DYS439", "DYS444", "DYS447", "DYS448", "DYS449", "DYS456", "DYS458", "DYS460", "DYS481", "DYS505", "DYS508", "DYS518", "DYS522", "DYS527a/b", "DYS533", "DYS549", "DYS557", "DYS570", "DYS576", "DYS593", "DYS596", "DYS612", "DYS626", "DYS627", "DYS635", "DYS643", "DYS645", "DYS722", "Y-GATA-H4", "DXS10074", "DXS10075", "DXS10079", "DXS101", "DXS10101", "DXS10103", "DXS10135", "DXS10148", "DXS10159", "DXS10162", "DXS10164", "DXS6789", "DXS6809", "DXS7132", "DXS7133", "DXS7423", "DXS7424", "DXS8378", "DXS981", "DXS9902", "GATA165B12", "GATA31E08", "HPRTB")
    List("男", "女").foreach { sex =>
      val strData = dicts.find(x => x("sex") == sex && x("kind") == "str").get
      val strDict = JSON.parse(strData("value")).toJDS
      val sexId = getSexId(sex)
      val strKindMap = Map(VarTool.autoSiteNames -> "autoStr", VarTool.ySiteNames -> "yStr", VarTool.xSiteNames -> "xStr")
      strKindMap.foreach { case (siteNames, strId) =>
        val html = strDict.filter(x => siteNames.contains(x._1)).map { case (k, v) =>
          div(cls := "form-group",
            div(cls := "myLabel", k),
            input(hidden, name := "strSiteNames[]", value := k),
            input(`type` := "text", id := "rg-from", name := "strSiteDatas[]", value := v, cls := "form-control",
              onblur := "PosSite.strUpdate(this)")
          )
        }.mkString("&nbsp;")
        $(s"#${sexId} #${strId}").html(html)
      }
    }
  }

  def refreshSnp(dicts: js.Array[js.Dictionary[String]]) = {
    List("男", "女").foreach { sex =>
      val strData = dicts.find(x => x("sex") == sex && x("kind") == "snp").get
      val strDict = JSON.parse(strData("value")).toJDS
      val html = strDict.map { case (k, v) =>
        div(cls := "form-group",
          div(cls := "myLabel", k),
          input(hidden, name := "snpSiteNames[]", value := k),
          input(`type` := "text", id := "rg-from", name := "snpSiteDatas[]", value := v, cls := "form-control",
            onblur := "PosSite.snpUpdate(this)")
        )
      }.mkString("&nbsp;")
      val sexId = getSexId(sex)
      $(s"#${sexId} #snp").html(html)
    }
  }

  @JSExport("myRun")
  def myRun = {
    val data = $(s"#form").serialize()
    val url = g.jsRoutes.controllers.AdminController.updatePosSite().url.toString
    val index = layer.alert(Tool.loadingElement, layerOptions)
    val formData = new FormData(document.getElementById("form").toHTMLFormElement)
    Ajax.post(url = s"${url}", data = formData,
      responseType = "application/json").map { xhr =>
      layer.close(index)
      layer.msg("更新成功！", LayerOptions.icon(1).time(1000))
    }
  }

  def getSex(sexId: String) = {
    sexMap.reverse(sexId)
  }

  @JSExport("strUpdate")
  def strUpdate(y: Element) = {
    val curForm = $(y).parents("form")
    val sexId = curForm.parent().attr("id").toString
    val sex = getSex(sexId)
    val data = curForm.serialize()
    val url = g.jsRoutes.controllers.AdminController.updatePosStrSite().url.toString
    val index = layer.alert(Tool.loadingElement, layerOptions)
    val formData = new FormData(document.getElementById("form").toHTMLFormElement)
    formData.append("sex", sex)
    Ajax.post(url = s"${url}", data = formData,
      responseType = "application/json").map { xhr =>
      layer.close(index)
      layer.msg("更新成功！", LayerOptions.icon(1).time(1000))
    }
  }

  @JSExport("snpUpdate")
  def snpUpdate(y: Element) = {
    val curForm = $(y).parents("form")
    val sexId = curForm.parent().attr("id").toString
    val sex = getSex(sexId)
    val data = curForm.serialize()
    val url = g.jsRoutes.controllers.AdminController.updatePosSnpSite().url.toString
    val index = layer.alert(Tool.loadingElement, layerOptions)
    val formData = new FormData(document.getElementById("form").toHTMLFormElement)
    formData.append("sex", sex)
    Ajax.post(url = s"${url}", data = formData,
      responseType = "application/json").map { xhr =>
      layer.close(index)
      layer.msg("更新成功！", LayerOptions.icon(1).time(1000))
    }
  }

}
